package com.shengzai.flink.source

import org.apache.flink.streaming.api.scala._
import org.apache.flink.api.common.eventtime.WatermarkStrategy
import org.apache.flink.api.common.serialization.SimpleStringSchema
import org.apache.flink.connector.kafka.source.KafkaSource
import org.apache.flink.connector.kafka.source.enumerator.initializer.OffsetsInitializer

object Demo4KafKaSource {
  def main(args: Array[String]): Unit = {

    val env: StreamExecutionEnvironment = StreamExecutionEnvironment.getExecutionEnvironment


    val source: KafkaSource[String] = KafkaSource
      .builder[String]
      .setBootstrapServers("master:9092,node1:9092,node2:9092") //kafka 节点列表
      .setTopics("bigdata") // 订阅Topics
      .setGroupId("my-group")   // 消费者组
      .setStartingOffsets(OffsetsInitializer.earliest) //从最末尾位点开始消费
      .setValueOnlyDeserializer(new SimpleStringSchema()).build  //指定反序列化的类

    val kafkaDS: DataStream[String] = env.fromSource(source, WatermarkStrategy.noWatermarks(), "Kafka Source")

    kafkaDS.print()

    env.execute()


  }

}
